package com.sample.demo.controller;

import ch.qos.logback.core.net.SyslogOutputStream;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sample.demo.entity.User;
import com.sample.demo.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@Slf4j
public class UserController {

    @Autowired
    private IUserService userService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Value("${server.port}")
    String port;


    @RequestMapping("/get")
    public String get(HttpServletRequest request){

        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");

        if(StringUtils.isEmpty(username)){
            username = "testSessionRedis: " + System.currentTimeMillis();
            session.setAttribute("username", username);
        }

        String result = "port:" + port + ", username" + username;
        try {
            Thread.sleep(20);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
//        System.out.println(result);

        return result;
    }

    @RequestMapping("/getAllUser")
    public List<User> getAllUser(){
        //log.info("get all user.....");

        List<User> users = userService.selectList(new QueryWrapper<User>().le("id", 10));

        return users;
    }

    @RequestMapping("/getAllUserRedis")
    public List<User> getAllUserRedis(){

        ListOperations<String, User> operations = redisTemplate.opsForList();
        List<User> users = null;
        if(redisTemplate.hasKey("users")){
            users = operations.range("users", 0, operations.size("users"));
//            System.out.println(users.size());
//            System.out.println("--------------->>>>>>");
        }else{

            users = userService.selectList(new QueryWrapper<User>().le("id", 10));
            operations.leftPushAll("users", users);
        }



        return users;
    }


    @RequestMapping("/redis")
    public String redis(){

        System.out.println("------------------");

        User user = new User();
        user.setId(10L);
        user.setUsername("zhangsan");
        user.setUsername("123456789");
        user.setDescription("hahaha");
        user.setHeadPhoto("/photo");
        ValueOperations<String, User> operations = redisTemplate.opsForValue();
        operations.set("user", user);

        String result = "not exists...";
        if(redisTemplate.hasKey("user")){
            User userGet = operations.get("user");
            result = "id:"+userGet.getId() + " username:"+user.getUsername() + " password:"+userGet.getPassword() + " description:"+userGet.getDescription();
            System.out.println(result);
        }else{
            System.out.println("not exists...");
        }


        return result;
    }




}
